package electrycity;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

@Slf4j
public class EleAPP1 {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        //建立job对象
        Job job = Job.getInstance(conf);

        //设置驱动类
        job.setJarByClass(EleAPP1.class);
        job.setJobName("task1");

        //设置mapper类
        job.setMapperClass(EleMapper.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        //设置reducer类
        job.setReducerClass(EleReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);


        //设置分区类
        job.setPartitionerClass(ElePartitioner.class);
        job.setNumReduceTasks(2);

        //设置输入输出文件路径，需要先删除文件目录
        String outDir = "file/electricity/out/task1";
        FileInputFormat.setInputPaths(job, new Path("file/electricity/electric.txt"));
        FileOutputFormat.setOutputPath(job, new Path(outDir));

        //提交程序
        job.waitForCompletion(true);
    }

    public static class EleMapper extends Mapper<LongWritable, Text, Text, Text> {
        /**
         * @param key     行偏移量
         * @param value   行内容
         * @param context 上下文对象，用于输出键值对
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
                throws IOException, InterruptedException {
            String[] split = value.toString().split(";");
            if (split.length == 11) {
                String year = split[2];
                String cf = split[8];
                String xyf = split[9];
                String ktf = split[10];
                if (!(year.equals("?") || cf.equals("?") || xyf.equals("?") || ktf.equals("?"))) {
                    String ele = cf + "\t" + xyf + "\t" + ktf;
                    context.write(new Text(year), new Text(ele));
                }
            }
        }
    }

    public static class EleReducer extends Reducer<Text, Text, Text, Text> {
        /**
         * @param key     年份
         * @param values  相同年份对应的所有数据
         * @param context 上下文对象，用于输出结果
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
                throws IOException, InterruptedException {
            double cfSum = 0;
            double xySum = 0;
            double ktSum = 0;
            double sum = 0;
            for (Text value : values) {
                String[] split = value.toString().split("\t");
                cfSum += Double.parseDouble(split[0]);
                xySum += Double.parseDouble(split[1]);
                ktSum += Double.parseDouble(split[2]);
            }
            sum += cfSum + xySum + ktSum;
            String ele = cfSum + "\t" + xySum + "\t" + ktSum + "\t" + sum;
            context.write(key, new Text(ele));
        }
    }
}
